package com.ruoyi.web.controller.admin.h5User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.H5User;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.dto.H5UserDetailDTO;
import com.ruoyi.system.service.IH5UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/h5user")
public class H5UserController extends BaseController {

    @Resource
    private IH5UserService h5UserService;

    /**
     * 获取h5用户列表
     */
    @GetMapping("/list")
    public TableDataInfo getH5UserList(@RequestParam(required = false) String userName,
                                       @RequestParam(required = false) String phone,
                                       @RequestParam(required = false) String startTime,
                                       @RequestParam(required = false) String endTime) {
        startPage();
        LambdaQueryWrapper<H5User> h5UserWrapper = Wrappers.lambdaQuery();
        h5UserWrapper.eq(H5User::getDelFlag, 0);
        if (StringUtils.isNotBlank(userName)) {
            h5UserWrapper.like(H5User::getUserName, userName);
        }
        if (StringUtils.isNotBlank(phone)) {
            h5UserWrapper.eq(H5User::getPhonenumber, phone);
        }
        if (StringUtils.isNotBlank(startTime)) {
            h5UserWrapper.ge(H5User::getCreateTime, startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            h5UserWrapper.le(H5User::getCreateTime, endTime);
        }
        List<H5User> list = h5UserService.list(h5UserWrapper);
        return getDataTable(list);
    }

    /**
     * 禁用H5用户账号
     */
    @PostMapping("/disable/{userId}")
    public AjaxResult disableH5User(@PathVariable Long userId) {
        try {
            log.info("管理员禁用H5用户账号，用户ID: {}", userId);
            boolean result = h5UserService.disableH5User(userId, "getUsername()");
            if (result) {
                return AjaxResult.success("禁用用户账号成功");
            } else {
                return AjaxResult.error("禁用用户账号失败");
            }
        } catch (Exception e) {
            log.error("禁用H5用户账号失败，用户ID: {}, 错误信息: {}", userId, e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 启用H5用户账号
     */
    @PostMapping("/enable/{userId}")
    public AjaxResult enableH5User(@PathVariable Long userId) {
        try {
            log.info("管理员启用H5用户账号，用户ID: {}", userId);
            boolean result = h5UserService.enableH5User(userId, "getUsername()");
            if (result) {
                return AjaxResult.success("启用用户账号成功");
            } else {
                return AjaxResult.error("启用用户账号失败");
            }
        } catch (Exception e) {
            log.error("启用H5用户账号失败，用户ID: {}, 错误信息: {}", userId, e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取H5用户详情（包含关联项目）
     */
    @GetMapping("/{userId}")
    public AjaxResult getH5UserDetail(@PathVariable Long userId) {
        try {
            log.info("获取H5用户详情，用户ID: {}", userId);
            H5UserDetailDTO detailDTO = h5UserService.getH5UserDetail(userId);
            return AjaxResult.success(detailDTO);
        } catch (Exception e) {
            log.error("获取H5用户详情失败，用户ID: {}, 错误信息: {}", userId, e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
